<?php
class newsControl extends commonControl{
	function index(){
        $db = K('newsview');
        $catid = isset($_GET['catid']) ? (int)$_GET['catid'] : false;
        $uid = isset($_GET['uid']) ? (int)$_GET['uid'] : false;
        $where_catid = $catid ? "news.catid = $catid" : '';
        $where_uid = $uid?"news.uid=$uid":'';
        $count = $db->where($where_catid)->where($where_uid)->where("recycled=0")->count();
        $page = new page($count, 2);
        $news = $db->field("id,hd_news.uid,username,title,click,news.catid,cname,addtime")->
                where($where_catid)->where($where_uid)->where("recycled=0")->order("id desc")->limit($page->limit())->all();
        $this->assign("news", $news);
        $this->assign("page", $page->show(2));
        $this->display();
	}
    function add_show(){//显示页面
        $this->assign("size","300,300");
        if (!isset($_GET['catid'])) {
            $catdb = M("category");
            $category = $catdb->field("catid,cname,pid")->all();
            $category = arr::channel($category, 'catid', 'pid', 0,0, 2);
            $this->assign("category", $category);
        }
        $this->display();
    }
    function add(){//新建方法
        $db = K("newsrelation");
        $_POST['addtime'] = time();
        $_POST['updatetime'] = time();
        $_POST['uid']=$_SESSION['user'];
        $_POST['thumb'] = isset($_POST['thumb'][1][0]) ? $_POST['thumb'][1][0] : '';
        if ($db->add()) {
            $this->success("文章添加成功!","index");
        } else {
            $this->error("文章添加失败");
        }
    }
    function edit_show() {
        $db = K("newsView");
        //获得文章编辑内容
        $news = $db->join("category,news_data")->find($_GET['id']);
        $this->assign("field", $news);
        //当前模型的所有栏目
        $category = $db->join()->table("category")->field("catid,cname,pid")->all();
        $category = arr::channel($category, 'catid', 'pid',0, 0, 2);
        $this->assign("category", $category);
        $this->display();
    }
    function edit() {
        $db = K("newsrelation");
        $_POST['updatetime'] = time();
        $_POST['thumb'] = $_POST['thumb'][1][0];
        if ($db->save()) {
            $this->success("文章修改成功! ", __CONTROL__);
        } else {
            $this->error("文章修改失败");
        }
    }
    //放入回收站
    function del() {
        $id = $_GET['id'];
        $db = M("news");
        if ($db->save(array("id" => $id, "recycled" => 1))) {
            $this->success("文件已放入回收站");
        } else {
            $this->error("删除文章失败");
        }
    }
    //查看回收站
    function recycled() {
        $db = K('newsview');
        $count = $db->where("recycled=1")->count();
        $page = new page($count, 2);
        $news = $db->join("category")->field("id,title,click,news.catid,cname,addtime")->
                        where("recycled=1")->limit($page->limit())->all();
        $this->assign("news", $news);
        $this->assign("page", $page->show(2));
        $this->display();
    }
    //还原文章
    function restore() {
        $id = $_GET['id'];
        $db = M("news");
        if ($db->save(array("id" => $id, "recycled" => 0))) {
            $this->success("文章已还原");
        } else {
            $this->error("还原文章失败，请重试");
        }
    }
    //彻底删除文章
    function del_news() {
        $id = $_GET['id'];
        $news_db = K("newsrelation");
        if ($news_db->join("news_data")->del($id)) {
            $this->success("删除成功");
        } else {
            $this->del("删除文章失败"
            );
        }
    }




















}
?>